package com.qztech.bluetooth;

import com.inuker.bluetooth.library.BluetoothClient;
import com.inuker.bluetooth.library.connect.response.BleWriteResponse;
import com.qztech.bluetooth.util.BLog;
import com.qztech.bluetooth.util.ByteUtil;
import com.qztech.utils.YzDebug;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;

public class WriteThread
        extends Thread {
    private boolean canWrite = true;
    private LinkedBlockingQueue<byte[]> mBuffers = new LinkedBlockingQueue<>();
    private ZBtDevice mDevice;
    private BluetoothClient mBluetoothClient;
    private final static long WRITE_WAIT = 30;
    private final static long WRITE_WAIT_LARGE = 30;
    private int mIndex = 0;

    public WriteThread(BluetoothClient client, ZBtDevice device) {
        setName("Write-thread-" + device.getName());
        mBluetoothClient = client;
        mDevice = device;
    }

    public void add(byte[] paramArrayOfByte) {
        this.mBuffers.add(paramArrayOfByte);
    }

    public int getCount() {
        if (this.mBuffers != null) {
            return this.mBuffers.size();
        }
        return 0;
    }

    @Override
    public void run() {
        byte[] arrayOfByte;
        while (this.canWrite) {
            try {
                arrayOfByte = this.mBuffers.take();
                writeNoRsp(arrayOfByte);
                mBuffers.remove(arrayOfByte);
                BLog.i("buffer", mBuffers.size() + "");
                if (mIndex % 2 == 0) {
                    Thread.sleep(WRITE_WAIT_LARGE);
                } else {
                    Thread.sleep(WRITE_WAIT);
                }
                mIndex++;
                if (mIndex == Integer.MAX_VALUE) {
                    mIndex = 0;
                }
            } catch (Exception localException) {
                this.canWrite = false;
                localException.printStackTrace();
            }
        }
        BLog.e("ble", "stop write");
    }

    public static boolean isWriteWithResponse(byte[] buffer) {
        return (buffer.length > 3 && buffer[0] == (byte) 0xaa && buffer[1] == (byte) 0x55 && buffer[2] == (byte) 0x47 && buffer[3] == (byte) 0x45);
    }

    public void stopWrite() {
        this.canWrite = false;
        mBuffers.clear();
        if (!isInterrupted()) {
            interrupt();
        }
    }

    private void writeNoRsp(final byte[] paramArrayOfByte)
            throws IOException {
        YzDebug.d("Ble", "writeNoRsp :" + ByteUtil.bytesToHexString(paramArrayOfByte) );
        mBluetoothClient.writeNoRsp(mDevice.getMac(), mDevice.getServiceUuid(), mDevice.getWriteCharacterUuid(), paramArrayOfByte, new BleWriteResponse() {
            public void onResponse(int paramAnonymousInt) {
                BLog.d("Ble", "writeNoRsp response:" + paramAnonymousInt + "content:" + ByteUtil.bytesToHexString(paramArrayOfByte));
            }
        });
    }

    private void writeWithResponse(final byte[] paramArrayOfByte)
            throws IOException {
        BLog.d("Ble", "writeWithResponse :" + ByteUtil.bytesToHexString(paramArrayOfByte));
        mBluetoothClient.write(mDevice.getMac(), mDevice.getServiceUuid(), mDevice.getWriteCharacterUuid(), paramArrayOfByte, new BleWriteResponse() {
            public void onResponse(int paramAnonymousInt) {
                BLog.d("Ble", "writeWithResponse response:" + paramAnonymousInt + "content:" + ByteUtil.bytesToHexString(paramArrayOfByte));
            }
        });
    }
}
